home *** CD-ROM | disk | FTP | other *** search
- /*
- ** File: iostream.h
- ** Description: buffered iostream class declarations
- **
- ** Digital Sound Machine v1.0
- ** © 1996 Dmitry Boldyrev
- **
- */
-
- #pragma once
-
- #define BIG_ENDIAN
-
- typedef char byte; // 8-bit signed
- typedef unsigned char ubyte; // 8-bit unsigned
- typedef short word; // 16-bit signed
- typedef unsigned short uword; // 16-bit unsigned
- typedef unsigned long ulong; // 32-bit unsigned
- typedef unsigned char bool; // boolean type (true/false)
- typedef word error; // error code (Mac compatible)
-
- // I/O Stream API (pure virtual)
-
- class ios
- {
- public:
-
- virtual ~ios() {}
-
- typedef enum {
- set = 0, // offset from start of file
- cur = 1, // offset from current position
- end = 2 // offset from end of file
- } ioPos;
-
- typedef enum {
- read = 0, // open file to read
- write = 1 // open file to write
- } ioMode;
-
- // -------------------------------------------------------------------------
-
- virtual byte fgetc() = 0;
- virtual byte fputc(byte c) = 0;
-
- virtual long fread(void *p, long size, long count) = 0;
- virtual long fwrite(void *p, long size, long count) = 0;
-
- virtual error feof() const = 0;
-
- virtual long ftell() = 0;
- virtual error fseek(long offset, ioPos whence) = 0;
-
- virtual error fflush() = 0;
-
- // -------------------------------------------------------------------------
-
- inline ubyte fgetb(); // motorolla/intel byte
-
- inline uword fgetmw(); // motorolla word
- inline uword fgetiw(); // intel word
-
- inline ulong fgetml(); // motorolla long
- inline ulong fgetil(); // intel long
-
- inline long freadi(void *p, long size, long count);
- inline long freadm(void *p, long size, long count);
-
- // -------------------------------------------------------------------------
-
- inline void fputb(byte b); // motorolla/intel byte
-
- inline void fputmw(word w); // motorolla word
- inline void fputiw(word w); // intel word
-
- inline void fputml(long l); // motorolla long
- inline void fputil(long l); // intel long
-
- inline error ferror(void) const; // returns the result of previous io operation
-
- protected:
-
- error err : 1;
-
- };
-
- // Memory stream
-
- class mstream : public ios
- {
- public:
-
- mstream();
- mstream(Ptr base, long length, ioMode mode);
- virtual ~mstream();
-
- virtual byte fgetc();
- virtual byte fputc(byte c);
-
- virtual long fread(void *p, long size, long count);
- virtual long fwrite(void *p, long size, long count);
-
- virtual error feof() const;
-
- virtual long ftell();
- virtual error fseek(long offset, ioPos whence);
-
- virtual error fflush();
-
- protected:
-
- Ptr hBase;
- Ptr hPos;
-
- long len;
-
- };
-
- // Resource stream
-
- class rstream : public mstream
- {
- public:
-
- rstream(ResType rType, short rID, ioMode mode);
- virtual ~rstream();
-
- protected:
-
- Handle hRsrc;
- };
-
-
- // File stream
-
- class fstream : public ios
- {
- public:
-
- fstream(const Str255 fName, short vRefNum, ioMode mode);
- fstream(const FSSpec &inSpec, ioMode mode);
- fstream(const SFReply &inReply, ioMode mode);
-
- virtual ~fstream();
-
- virtual byte fgetc();
- virtual byte fputc(byte c);
-
- virtual long fread(void *p, long size, long count);
- virtual long fwrite(void *p, long size, long count);
-
- virtual error feof() const;
-
- virtual long ftell();
- virtual error fseek(long offset, ioPos whence);
-
- virtual error fflush();
-
- protected:
-
- enum {
- IO_BUFSIZE = 512,
- IO_EOF = -1
- };
-
- void Init();
-
- byte __read();
- byte __write();
- void __buffer();
- void __reset();
-
- unsigned char* ptr; // buffer incremental pointer
- char* buf; // buffer mem location
- long size; // buffer size
-
- long cnt; // increment count
- long pos; // position in a file
- long len; // length of a file
-
- unsigned pushed : 1; // buffer pushed flag
- unsigned eof : 1; // end of file flag
-
- short refnum; // file reference number
- };
-
- inline error ios::ferror(void) const
- {
- return err;
- }
-
- inline error fstream::feof() const
- {
- return eof;
- }
- inline ubyte ios::fgetb()
- {
- return fgetc();
- }
- inline uword ios::fgetiw()
- {
- return (((uword)fgetb())<<8)|((uword)fgetb());
- }
- inline uword ios::fgetmw()
- {
- return ((uword)fgetb())|(((uword)fgetb())<<8);
- }
- inline ulong ios::fgetml()
- {
- return ((ulong)fgetmw())|(((ulong)fgetmw())<<16);
- }
- inline ulong ios::fgetil()
- {
- return (((ulong)fgetiw())<<16)|((ulong)fgetiw());
- }
- inline void ios::fputb(byte b)
- {
- fputc(b);
- }
- inline void ios::fputmw(word w)
- {
- fputb(w&0xff); fputb(w>>8);
- }
- inline void ios::fputiw(word w)
- {
- fputb(w>>8); fputb(w&0xff);
- }
- inline void ios::fputml(long l)
- {
- fputmw(l&0xffff); fputmw(l>>16);
- }
- inline void ios::fputil(long l)
- {
- fputiw(l>>16); fputiw(l&0xffff);
- }
- #define FLIP16(a) ((a)=(a>>8)&0xff|(a<<8))
- #define FLIP32(a) ((a)=((a&0xFF000000)>>24)|((a&0x00FF0000)>>8)| \
- ((a&0x0000FF00)<<8)|(a<<24))
-
- inline long ios::freadi(void *p, long size, long count)
- {
- long result = fread(p, size, count);
- #ifdef BIG_ENDIAN
- switch (size)
- {
- case 2:
- word *wptr = (word*) p;
- for(; count>0; count--, wptr++)
- FLIP16(*wptr);
- break;
- case 4:
- long *lptr = (long*) p;
- for(; count>0; count--, lptr++)
- FLIP32(*lptr);
- break;
- }
- #endif
- return result;
- }
- inline long ios::freadm(void *p, long size, long count)
- {
- long result = fread(p, size, count);
- #ifndef BIG_ENDIAN
- switch (size)
- {
- case 2:
- word *wptr = (word*) p;
- for(; count>0; count--, wptr++)
- FLIP16(*wptr);
- break;
- case 4:
- long *lptr = (long*) p;
- for(; count>0; count--, lptr++)
- FLIP32(*lptr);
- break;
- }
- #endif
- return result;
- }
-
-
-